import { buffer, stream } from "@kit.ArkTS";

export class Collector extends stream.Writable {
  public bufferedBytes: Uint8Array[] = [];
  doWrite(chunk: string | Uint8Array, encoding: string, callback: Function): void {
    if (typeof chunk ==='string') {
      if (buffer.isEncoding(encoding)) {
        this.bufferedBytes.push(new Uint8Array(buffer.from(chunk,encoding as buffer.BufferEncoding).buffer))
      }else{
        this.bufferedBytes.push(new Uint8Array(buffer.from(chunk).buffer))
      }
    }else if (chunk instanceof Uint8Array){
      this.bufferedBytes.push(chunk)
    }
    callback();
  }
}
